/////-----开票管理页面   其他操作  ----------


import * as Utils from '@/utils/util'
import { getConfirmDelivery, getInvoiceRevoke,returnInvoicePayOrder } from '@api/invoiceAdminister'
import InvoiceDownloadFormat from '@views/invoice/common/invoiceDownloadFormat.vue'

const otherButtonEvents = {
  components: {
    InvoiceDownloadFormat
  },
  data(){
    return{
      //----------撤销并删除----------
      deleteArr: [],  //撤销并删除数组
      testArr: [],
      //----------发票领用数量弹层--------
      theData: {},  //订单信息
      ticketMargin: {}, //发票数据
      undeleteInfo:{
        isCollecteShow: false,   //发票领用弹层
        blInvcList: [],  //补领发票数组
      },
      //---------待支付撤销---------
      //支付撤销
      payStatus: false,
      payCheckList: [],  //支付撤销数据
      payRevokeConfig:{
        width: '800px',
        mark: 1,
        visible: false,
      },
      //下载发票
      downloadFormatInfo: {
        visible: false,
        id: null
      },
      //开票失败
      revokeFlagStatus: false,  //是否包含开票失败的票

    }
  },
  methods: {
    //补充资料
    suppleMentary(record){
      this.$router.push({
        path: '/invoice/applyInvoice',
        query:{
          bizId: record.entId,
          detailsId: record.id,
        }
      })
    },
    //删除草稿
    deleteDraft(record){
      this.id = record.id;
      this.theData = record;
      let arr = [];
      arr.push(this.id)
      this.deleteArr = arr;

      this.$nextTick(()=>{
        this.undoConfig = {
          width: '500px',  //modal宽度
          visible: true,    //modal展示状态
          modalTitle: '',
          title: '',     //标题
          subTitle: '您确定删除该草稿吗？',     //副标题
          content: '',  //内容
          okText: '确定删除',    //确定文字
          okType: 'danger',    //确定类型
          cancleText: '暂不删除',    //取消文字
          cancleType: 'default',    //取消类型
          source: 4,    //来源
        }
      })
    },
    //-------------------------撤销并删除与批量删除------------------
    //撤销并删除
    undoDelete(record){
      this.id = record.id;
      this.theData = record;
      let arr = [];
      arr.push(this.id)
      this.deleteArr = arr;

      this.$nextTick(()=>{
        this.undoConfig.visible = true;
      })
    },
    //批量删除
    batchDetaleFun(){
      this.deleteArr = this.reorganize('payOrder',this.selectedRows);

      if (this.selectedRowKeys.length > 0){
        this.$nextTick(()=>{
          this.undoConfig.visible = true;
        })
      }else {
        this.$message.warning('合并删除选中数据不能为空');
        return false;
      }
    },
    //请求撤销删除接口
    undeleteOperate(){
      let params = {};
      params.blInvcList = this.undeleteInfo.blInvcList;
      params.cancleInvcList = this.deleteArr;
      getInvoiceRevoke(params).then(res => {
        if (res.success) {
          this.$success({
            title: '撤销成功',
            okText: '确认'
          });
          this.getTableData();
        }else{
          this.$message.warning(res.message);
        }
      })
    },
    //-------------------------支付与批量支付  start ↓------------------
    //支付
    paymentOther(record){
      let arr = [];
      arr.push(record.payOrder)
      this.paymentInvoice(arr);
    },
    //批量支付
    batchPaymentFun(){
      let arr = this.reorganize('payOrder',this.selectedRows);
      if (this.selectedRowKeys.length > 0){
        this.paymentInvoice(arr);
      }else {
        this.$message.warning('合并支付选中数据不能为空');
        return false;
      }
    },
    //支付接口
    paymentInvoice(arr){
      let key = 'paymentInvoice'
      Utils.Storage.set(key,{orderNo: arr});
      // console.log(Utils.Storage.get('paymentInvoice'))
      this.$router.push({
        path: '/myAccount/accountManage',
        query:{
          po:key
        }
      });
    },
    //-------------------------支付状态下的撤销  start ↓------------------
    //支付撤销
    paymentRevoke(record){
      //判断当前票是否是开票失败过来的票
      if (record.errorFlag && record.errorFlag > 1){
        //调用撤销支付接口
        this.revokeAll(record.payOrder);
      }else {
        this.entId = null;
        this.entId = record.entId;
        this.payStatus = true;
        //调用获取支付单判断
        this.modifyAsynchronous();
      }
    },
    //全部撤销
    revokeAll(payOrder){
      returnInvoicePayOrder(payOrder).then(res => {
        if (res.success) {
          if (res.result){
            this.$message.success('撤销成功');
            //刷新列表
            this.getTableData();
          }
        }else{
          this.$message.warning(res.message);
        }
      })
    },
    //支付撤销弹层 结算按钮成功回调
    settlePayMethod(config){
      this.$nextTick(()=> {
        this.payRevokeConfig.visible = false;
      })
      this.digitalInfo.mark = 1;  //待支付过来的结算

      let newList = config.payCheckList;
      let checkList = newList.filter(item => item.checkShow == true);
      // console.log('支付撤销弹层结算数据',newList)
      this.overLimitCheckList = checkList;
      this.balanceCheckList = checkList.map(item => ({ ...item, payCheckStatus: true }));  //增加表示  区分状态

      let payList = newList.filter(item => item.checkShow == false);  //有支付订单

      this.digitalInfo.pageSource = 4;

      if (payList.length > 0){
        this.digitalInfo.bePaidStatus = true;  //有待支付订单
        this.toBePaidList = newList.filter(item => item.checkShow == false);  //有支付订单
      }else{
        this.digitalInfo.bePaidStatus = false; //无待支付订单
        this.toBePaidList = [];  //待支付订单数据
      }
      this.digitalInfo.moneyTotal = this.balanceCheckList.reduce((sum, item) => { return sum + item.amount }, 0);

      //比对该企业总和是否超限额
      this.limitTotal();

    },
    //全部撤销成功回调
    cancelRevoke(){
      this.$nextTick(()=> {
        this.payRevokeConfig.visible = false;
      })
      this.payStatus = false;

      this.getTableData();
    },
    //-------------------------确认寄送与下载发票  start ↓------------------
    //确认寄送
    confirmDelivery(id){
      getConfirmDelivery(id).then(res => {
        if (res.success) {
          this.$nextTick(()=>{
            this.undoConfig = {
              width: '500px',  //modal宽度
              visible: true,    //modal展示状态
              modalTitle: '',
              title: '发送成功！',     //标题
              subTitle: '',     //副标题
              content: '',  //内容
              okText: '确定',    //确定文字
              okType: 'primary',    //确定类型
              cancleText: '',    //取消文字
              cancleType: '',    //取消类型
              source: '',    //来源
            }
          })
          this.getTableData();
        }else{
          this.$message.warning(res.message);
        }
      })
    },
    //下载发票
    invoiceDownload(data){
      this.$nextTick(()=>{
        this.downloadFormatInfo.visible = true;
        this.downloadFormatInfo.id = data.id;
      })
    },
    //关闭
    handleInvoiceCancle(){
      this.downloadFormatInfo.visible = false;
    },
    //-------------------------公共方法  start ↓------------------
    //变更重组
    reorganize(name,data){
      let arr = [];
      for (const key in data) {
        let dataNew = data[key];
        Object.keys(dataNew).map((key)=>{
          if (key == name){
            arr.push(dataNew[key]);
          }
        });
      }
      return arr;
    },

  }
}

export  default  otherButtonEvents
